package com.ng.desktop.view.dieta;

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import com.ng.framework.view.desktop.NGGUIUtils;

public class DietaFrame extends JInternalFrame implements ActionListener {
	
	private static final long serialVersionUID = 4091543302652512373L;
	
	private String PARAMETROS_INICIALES = "parametros_iniciales";
	private String PORCIONES = "porciones";
	private String COMIDAS = "comidas";
	private String DIETA = "dieta";
	
	private JPanel panelPrincipal;
	private CardLayout cardLayout;
	private JButton buttonSiguiente;
	private JButton buttonAnterior;
	private JButton buttonVolver;
	private ParametrosInicialesPanel parametrosInicialesPanel;
	private PorcionesPanel porcionesPanel;
	private ComidasPanel comidasPanel;
	private DietaPanel dietaPanel;
	private String visiblePanel;
	
	public DietaFrame() {
		this.buildGui();
		
		this.setFrameIcon(NGGUIUtils.getImageIcon("icons/balance.png"));
		this.setTitle("Dietas");
		this.setSize(new Dimension(1300, 600));
		this.setResizable(true);
		
		this.getParamentrosInicialesPanel().updateTable();
	}
	
	private void buildGui() {
		Container contentPane = this.getContentPane();
		contentPane.setLayout(new GridBagLayout());
		
		this.add(this.getPanelPrincipal(), new GridBagConstraints(0, 0, 4, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
		this.add(this.getButtonAnterior(), new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
		this.add(this.getButtonSiguiente(), new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		this.add(new JSeparator(JSeparator.VERTICAL), new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0, 0, 5, 5), 0, 0));
		this.add(this.getButtonVolver(), new GridBagConstraints(3, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));	
	}
	
	private JPanel getPanelPrincipal() {
		if(panelPrincipal == null) {
			this.panelPrincipal = new JPanel(this.getCardLayout());
			panelPrincipal.add(this.getParamentrosInicialesPanel(), PARAMETROS_INICIALES);
			panelPrincipal.add(this.getPorcionesPanel(), PORCIONES);
			panelPrincipal.add(this.getComidasPanel(), COMIDAS);
			panelPrincipal.add(this.getDietaPanel(), DIETA);
			
			this.visiblePanel = PARAMETROS_INICIALES;
		}
		return panelPrincipal;
	}
	
	private CardLayout getCardLayout() {
		if(cardLayout == null) {
			this.cardLayout = new CardLayout();
		}
		return cardLayout;
	}
	
	private ParametrosInicialesPanel getParamentrosInicialesPanel() {
		if(parametrosInicialesPanel == null) {
			this.parametrosInicialesPanel = new ParametrosInicialesPanel(this.getButtonSiguiente());
		}
		return parametrosInicialesPanel;
	}
	
	private PorcionesPanel getPorcionesPanel() {
		if(porcionesPanel == null) {
			this.porcionesPanel = new PorcionesPanel();
		}
		return porcionesPanel;
	}
	
	private ComidasPanel getComidasPanel() {
		if(comidasPanel == null) {
			this.comidasPanel = new ComidasPanel();
		}
		
		return comidasPanel;
	}
	
	private DietaPanel getDietaPanel() {
		if(dietaPanel == null) {
			this.dietaPanel = new DietaPanel();
		}
		return dietaPanel;
	}
	
	private JButton getButtonAnterior() {
		if(buttonAnterior == null) {
			this.buttonAnterior = NGGUIUtils.createTextButton("Anterior", this);
			buttonAnterior.setEnabled(false);
			buttonAnterior.setMinimumSize(buttonAnterior.getPreferredSize());
		}
		return buttonAnterior;
	}
	
	private JButton getButtonSiguiente() {
		if(buttonSiguiente == null) {
			this.buttonSiguiente = NGGUIUtils.createTextButton("Siguiente", this);
			buttonSiguiente.setEnabled(false);
			buttonSiguiente.setMinimumSize(buttonSiguiente.getPreferredSize());
		}
		return buttonSiguiente;
	}
	
	private JButton getButtonVolver() {
		if(buttonVolver == null) {
			this.buttonVolver = NGGUIUtils.createTextButton("Volver", this);
			buttonVolver.setMinimumSize(buttonVolver.getPreferredSize());
		}
		return buttonVolver;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.getButtonSiguiente()) {
			if(PARAMETROS_INICIALES.equals(visiblePanel)) {
				this.getPorcionesPanel().setChon(this.getParamentrosInicialesPanel().getChon());
				this.getPorcionesPanel().setCho(this.getParamentrosInicialesPanel().getCho());
				this.getPorcionesPanel().setFat(this.getParamentrosInicialesPanel().getFat());
				this.getPorcionesPanel().setEnergia(this.getParamentrosInicialesPanel().getEnergia());
				this.getPorcionesPanel().updateTable();
				this.visiblePanel = PORCIONES;
				this.getButtonAnterior().setEnabled(true);
			} else if(PORCIONES.equals(visiblePanel)) {
				this.getComidasPanel().setPorciones(this.getPorcionesPanel().getPorciones());
				this.getComidasPanel().setEnergia(this.getPorcionesPanel().getEnergia());
				this.getComidasPanel().updateTable();
				this.visiblePanel = COMIDAS;
			} else if(COMIDAS.equals(visiblePanel)){
				this.getDietaPanel().setPaciente(this.getParamentrosInicialesPanel().getPaciente());
				this.getDietaPanel().setCalorias(this.getParamentrosInicialesPanel().getCalorias());
				this.getDietaPanel().generarDieta(this.getParamentrosInicialesPanel().getDuracion(), this.getComidasPanel().getPorcionesComida());
				this.getButtonSiguiente().setEnabled(false);
				this.visiblePanel = DIETA;
			}			
			cardLayout.next(this.getPanelPrincipal());
		} else if(e.getSource() == this.getButtonAnterior()) {
			boolean volver = true;
			if(PORCIONES.equals(visiblePanel)) {
				this.visiblePanel = PARAMETROS_INICIALES;
				this.getButtonAnterior().setEnabled(false);
			} else if(COMIDAS.equals(visiblePanel)){
				this.visiblePanel = PORCIONES;			
			} else if(DIETA.equals(visiblePanel)) {
				this.visiblePanel = COMIDAS;
			}
			if(volver) {
				this.getButtonSiguiente().setEnabled(true);
				cardLayout.previous(this.getPanelPrincipal());				
			}
		} else if(e.getSource() == this.getButtonVolver()) {
			this.dispose();
		}
	}
	
}
